package com.beyond.trace.service.impl.warn;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beyond.trace.config.IotConfig;
import com.beyond.trace.domain.dto.warn.WarnRecordDTO;
import com.beyond.trace.domain.dto.warn.WarnRecordListDTO;
import com.beyond.trace.domain.enums.warn.*;
import com.beyond.trace.domain.pojo.warn.WarnRecordPageQuery;
import com.beyond.trace.domain.warn.WarnRecord;
import com.beyond.trace.domain.warn.WarnRule;
import com.beyond.trace.mapper.WarnRecordMapper;
import com.beyond.trace.service.impl.AliyunSmsService;
import com.beyond.trace.service.warn.WarnRecordService;
import com.beyond.trace.service.warn.WarnRuleService;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: WarnRecordServiceImpl
 * @author: z越
 * @date: 2023/2/24  9:59
 * @description 预警记录接口实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class WarnRecordServiceImpl extends ServiceImpl<WarnRecordMapper, WarnRecord> implements WarnRecordService {
    private final WarnRuleService warnRuleService;

    @Autowired
    private RedisCache redisCache;
    private final AliyunSmsService aliyunSmsService;

    @Autowired
    private IotConfig iotConfig;

    @Override
    public Page<WarnRecordListDTO> findAllPages(WarnRecordPageQuery query,boolean valid) {
        Page<WarnRecord> page = new Page<>(query.getPageNum(), query.getPageSize());
        String startTime="";
        String endTime="";
        if (query.getWarnTime() != null){
            Map<String, String> day = getFirstDayAndLastDayOfTheSpecifiedMonth(query.getWarnTime());
            startTime = day.get("start");
            endTime = day.get("end");
        }
        String departId = "";
        if(com.ruoyi.common.utils.StringUtils.isNotEmpty(query.getDepartId())) {
            departId = query.getDepartId();
        }
        if(valid) {
            if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
                departId = String.valueOf(SecurityUtils.getUserDepartId());
            }
        }
        return this.baseMapper.findAllPages(page, query.getWarnType(),query.getDeviceName(),query.getWarnGrade(),startTime,endTime, departId);
    }

    @Override
    public WarnRecordDTO findById(Long id) {
        WarnRecordDTO result = new WarnRecordDTO();
        WarnRecord warnRecord = this.baseMapper.selectById(id);
        if (warnRecord != null) {
            BeanUtils.copyBeanProp(result, warnRecord);
            //查询预警规则数据
            WarnRule warnRule = this.warnRuleService.getById(warnRecord.getWarnId());
            if (warnRule != null){
                result.setDeviceType(warnRule.getDeviceType());
                result.setPropose(warnRule.getPropose());
                result.setRemark(warnRule.getRemark());
                result.setWarnPeriod(warnRule.getWarnPeriod());
                result.setWarnFactor(warnRule.getWarnFactor());
            }
        }
        return result;
    }

    @Override
    public void insertWarnRecord() {
        this.saveBatch(passRuleInsertRecord());
    }

    //根据规则插入预警记录数据
    private List<WarnRecord> passRuleInsertRecord() {
        List<WarnRecord> warnRecords = new ArrayList<>();
        List<WarnRule> list = this.warnRuleService.list();
        if (list.size() > 0) {
            for (WarnRule warnRule : list) {
                if ("OFF".equals(warnRule.getStatus())){
                    continue;
                }
                String[] phones = warnRule.getPhone().split(",");//获取电话号码
                Object cacheObject = redisCache.getCacheObject(warnRule.getDeviceNo()+":"+warnRule.getId());//判断设备在预警周期内有没有发过消息，如果发过了 就不发短息了
                if (cacheObject == null) {
                    WarnRecord warnRecord = new WarnRecord();
                    warnRecord.setWarnTime(new Date());
                    warnRecord.setWarnGrade(warnRule.getWarnGrade());
                    warnRecord.setCreateTime(new Date());
                    warnRecord.setDeviceName(warnRule.getDeviceName());
                    warnRecord.setWarnId(warnRule.getId());
                    warnRecord.setWarnType(warnRule.getWarnType());
                    warnRecord.setDepartId(warnRule.getDepartId());
                    WarnTypeEnum warnTypeEnum = WarnTypeEnum.valueOf(warnRule.getWarnType());
                    switch (warnTypeEnum) {//根据类型调用不同的方法处理数据
                        case QI_XIANG:
                            String str = handleQxData(warnRule);
                            if (StringUtils.isNotEmpty(str)) {//如果值为空 则证明不需要预警
                                warnRecord.setWarnValue(str);
                                warnRecords.add(warnRecord);
                                int time = Integer.parseInt(warnRule.getWarnPeriod()
                                        .substring(0, warnRule.getWarnPeriod().length()-2));
                                redisCache.setCacheObject(warnRule.getDeviceNo()+":"+warnRule.getId(), warnRule.getWarnPeriod(),
                                        time * 60, TimeUnit.MINUTES);//用redis做记录 在周期内只保留一次记录
                                sendSms(phones, warnRule, "气象站","气象预警");
                            }
                            break;
                        case SHANG_QING:
                            String qx = handleSqData(warnRule);
                            if (StringUtils.isNotEmpty(qx)) {//如果值为空 则证明不需要预警
                                warnRecord.setWarnValue(qx);
                                warnRecords.add(warnRecord);
                                int time = Integer.parseInt(warnRule.getWarnPeriod()
                                        .substring(0, warnRule.getWarnPeriod().length()-2));
                                redisCache.setCacheObject(warnRule.getDeviceNo()+":"+warnRule.getId(), warnRule.getWarnPeriod(),
                                        time * 60, TimeUnit.MINUTES);//用redis做记录 在周期内只保留一次记录
                                sendSms(phones, warnRule, "墒情监测站","墒情预警");
                            }
                            break;
                        case SHUI_ZHI:
                            String sz = handleSzData(warnRule);
                            if (StringUtils.isNotEmpty(sz)) {//如果值为空 则证明不需要预警
                                warnRecord.setWarnValue(sz);
                                warnRecords.add(warnRecord);
                                int time = Integer.parseInt(warnRule.getWarnPeriod()
                                        .substring(0, warnRule.getWarnPeriod().length()-2));
                                redisCache.setCacheObject(warnRule.getDeviceNo()+":"+warnRule.getId(), warnRule.getWarnPeriod(),
                                        time * 60, TimeUnit.MINUTES);//用redis做记录 在周期内只保留一次记录
                                sendSms(phones, warnRule, "水质监测站","水质预警");
                            }
                            break;
                        case KONG_QI:
                            String kq = handleKqData(warnRule);
                            if (StringUtils.isNotEmpty(kq)) {//如果值为空 则证明不需要预警
                                warnRecord.setWarnValue(kq);
                                warnRecords.add(warnRecord);
                                int time = Integer.parseInt(warnRule.getWarnPeriod()
                                        .substring(0, warnRule.getWarnPeriod().length()-2));
                                redisCache.setCacheObject(warnRule.getDeviceNo()+":"+warnRule.getId(), warnRule.getWarnPeriod(),
                                        time * 60, TimeUnit.MINUTES);//用redis做记录 在周期内只保留一次记录
                                sendSms(phones, warnRule, "空气质量监测站","空气质量预警");
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        return warnRecords;
    }

    private void sendSms(String[] phones, WarnRule warnRule, String deviceType,String alarmType) {
        String time = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", new Date());
        String message = String.format(AliyunSmsService.ALARM_TEMPLATE_CODE, deviceType, time,alarmType);
        if (phones.length > 0) {
            for (String phone : phones) {
                try{
                    aliyunSmsService.send(phone, message);
                }catch (Exception e){
                    log.error("发送短信失败：{}",e.getMessage());
                }
            }
        }
    }

    //处理气象设备数据
    private String handleQxData(WarnRule warnRule) {
        String deviceData = findQxDeviceData(warnRule.getDeviceNo());
        if (Objects.isNull(deviceData)){
            return null;
        }
        JSONObject deviceJson = JSON.parseObject(deviceData);
        JSONObject data = deviceJson.getJSONObject("data");
        JSONArray dataList = data.getJSONArray("dataList");
        if(dataList.isEmpty()) {
            return null;
        }
        JSONObject deviceCurrentData = dataList.getJSONObject(0);

        float min = Float.parseFloat(warnRule.getMinValue());
        float max = Float.parseFloat(warnRule.getMaxValue());
        QxFactorEnum qxFactorEnum = QxFactorEnum.valueOf(warnRule.getWarnFactor());
        switch (qxFactorEnum) {//根据预警因子判断设备值是否正确
            case TEMP:
                if(deviceCurrentData.getDouble("tmp") != null) {
                    Double value = deviceCurrentData.getDouble("tmp");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case HUM:
                if(deviceCurrentData.getDouble("hmdy") != null) {
                    Double value = deviceCurrentData.getDouble("hmdy");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case HV:
                if(deviceCurrentData.getDouble("lux") != null) {
                    Double value = deviceCurrentData.getDouble("lux");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case WIND_SPEED:
                if(deviceCurrentData.getDouble("wind") != null) {
                    Double value = deviceCurrentData.getDouble("wind");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case TOTAL_RAINFALL:
                if(deviceCurrentData.getDouble("rain20min") != null) {
                    Double value = deviceCurrentData.getDouble("rain20min");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case CO2:
                if(deviceCurrentData.getDouble("co2") != null) {
                    Double value = deviceCurrentData.getDouble("co2");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case PM25:
                if(deviceCurrentData.getDouble("pm25") != null) {
                    Double value = deviceCurrentData.getDouble("pm25");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case RADIO:
                if(deviceCurrentData.getDouble("tbq") != null) {
                    Double value = deviceCurrentData.getDouble("tbq");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            default:
                break;
        }
        return null;
    }

    //处理水质监测站数据
    private String handleSzData(WarnRule warnRule) {
        String deviceData = findSzDeviceData(warnRule.getDeviceNo());
        if (Objects.isNull(deviceData)){
            return null;
        }
        JSONObject deviceJson = JSON.parseObject(deviceData);
        JSONObject data = deviceJson.getJSONObject("data");
        JSONArray dataList = data.getJSONArray("dataList");
        if(dataList.isEmpty()) {
            return null;
        }
        JSONObject deviceCurrentData = dataList.getJSONObject(0);
        double min = Double.parseDouble(warnRule.getMinValue());
        double max = Double.parseDouble(warnRule.getMaxValue());
        SzFactorEnum szFactorEnum = SzFactorEnum.valueOf(warnRule.getWarnFactor());
        switch (szFactorEnum) {//根据预警因子判断设备值是否正确
            case SLUDGE:
                if(deviceCurrentData.getDouble("turbidity") != null) {
                    Double value = deviceCurrentData.getDouble("turbidity");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case WATER_TEMP:
                if(deviceCurrentData.getDouble("waterT") != null) {
                    Double value = deviceCurrentData.getDouble("waterT");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case PH:
                if(deviceCurrentData.getDouble("waterPH") != null) {
                    Double value = deviceCurrentData.getDouble("waterPH");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case RESIDUAL_CHLORINE:
                if(deviceCurrentData.getDouble("Rchlorine") != null) {
                    Double value = deviceCurrentData.getDouble("Rchlorine");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case WATER_LEVEL:
                if(deviceCurrentData.getDouble("waterlevel") != null) {
                    Double value = deviceCurrentData.getDouble("waterlevel");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case CONDUCTIVITY:
                if(deviceCurrentData.getDouble("waterconductivity") != null) {
                    Double value = deviceCurrentData.getDouble("waterconductivity");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case DISSOLVED_OXYGEN:
                if(deviceCurrentData.getDouble("dissolvedoxygen") != null) {
                    Double value = deviceCurrentData.getDouble("dissolvedoxygen");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            default:
                break;
        }
        return null;
    }

    //处理墒情监测站数据
    private String handleSqData(WarnRule warnRule) {
        String deviceData = findSqDeviceData(warnRule.getDeviceNo());
        if (Objects.isNull(deviceData)){
            return null;
        }
        JSONObject deviceJson = JSON.parseObject(deviceData);
        JSONObject data = deviceJson.getJSONObject("data");
        JSONArray dataList = data.getJSONArray("dataList");
        if(dataList.isEmpty()) {
            return null;
        }
        JSONObject deviceCurrentData = dataList.getJSONObject(0);

        double min = Double.parseDouble(warnRule.getMinValue());
        double max = Double.parseDouble(warnRule.getMaxValue());
        SqFactorEnum sqFactorEnum = SqFactorEnum.valueOf(warnRule.getWarnFactor());
        switch (sqFactorEnum) {//根据预警因子判断设备值是否正确
            case SOIL_TERM:
                if(deviceCurrentData.getDouble("tmp1") != null) {
                    Double value = deviceCurrentData.getDouble("tmp1");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case SOIL_HUM:
                if(deviceCurrentData.getDouble("hmdy1") != null) {
                    Double value = deviceCurrentData.getDouble("hmdy1");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case SOIL_PH:
                if(deviceCurrentData.getDouble("ph") != null) {
                    Double value = deviceCurrentData.getDouble("ph");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case SOIL_EC:
                if(deviceCurrentData.getDouble("ec1") != null) {
                    Double value = deviceCurrentData.getDouble("ec1");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case SOIL_NIT:
                if(deviceCurrentData.getDouble("n") != null) {
                    Double value = deviceCurrentData.getDouble("n");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case SOIL_PHOS:
                if(deviceCurrentData.getDouble("p") != null) {
                    Double value = deviceCurrentData.getDouble("p");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case SOIL_KAL:
                if(deviceCurrentData.getDouble("k") != null) {
                    Double value = deviceCurrentData.getDouble("k");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            default:
                break;
        }
        return null;
    }

    //处理空气质量监测站数据
    private String handleKqData(WarnRule warnRule) {
        String deviceData = findSqDeviceData(warnRule.getDeviceNo());
        if (Objects.isNull(deviceData)){
            return null;
        }
        JSONObject deviceJson = JSON.parseObject(deviceData);
        JSONObject data = deviceJson.getJSONObject("data");
        JSONArray dataList = data.getJSONArray("dataList");
        if(dataList.isEmpty()) {
            return null;
        }
        JSONObject deviceCurrentData = dataList.getJSONObject(0);

        double min = Double.parseDouble(warnRule.getMinValue());
        double max = Double.parseDouble(warnRule.getMaxValue());
        KqFactorEnum kqFactorEnum = KqFactorEnum.valueOf(warnRule.getWarnFactor());
        switch (kqFactorEnum) {//根据预警因子判断设备值是否正确
            case PRESSURE:
                if(deviceCurrentData.getDouble("pressure") != null) {
                    Double value = deviceCurrentData.getDouble("pressure");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case NOISE:
                if(deviceCurrentData.getDouble("noise") != null) {
                    Double value = deviceCurrentData.getDouble("noise");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case PM10:
                if(deviceCurrentData.getDouble("pm10") != null) {
                    Double value = deviceCurrentData.getDouble("pm10");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case NO2:
                if(deviceCurrentData.getDouble("NO2") != null) {
                    Double value = deviceCurrentData.getDouble("NO2");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case PM25:
                if(deviceCurrentData.getDouble("pm25") != null) {
                    Double value = deviceCurrentData.getDouble("pm25");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            case OZONE:
                if(deviceCurrentData.getDouble("O3") != null) {
                    Double value = deviceCurrentData.getDouble("O3");
                    if(value > min || value < max) {
                        return value.toString();
                    }
                }
                break;
            default:
                break;
        }
        return null;
    }

    //查询气象设备数据
    private String findQxDeviceData(String deviceNo) {
        String token = getToken();
        String url = iotConfig.getUrl() + "/product/productDevice/getDeviceCurrentData";
        try {
            Map<String,String> header = new HashMap<>();
            header.put("Authorization",token);
            header.put("cvn-user-id",redisCache.getCacheObject("YSSYUSERID"));
            header.put("issuer","enroll");
            JSONObject param = createParams(deviceNo);
            String result = HttpRequest.post(url).addHeaders(header).body(param.toJSONString()).execute().body();
            JSONObject jsonObject = JSON.parseObject(result);
            if (!"200".equals(jsonObject.getString("code"))) {
                log.info("请求绿色云农接口失败:{},{}", jsonObject.getString("msg"),url);
            } else {
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private JSONObject createParams(String deviceNo){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, String> times = getFirstDayAndLastDayOfTheSpecifiedMonth(sdf.format(new Date()));
        JSONObject param = new JSONObject();
        param.put("deviceId",deviceNo);
        param.put("pageNum",1);
        param.put("pageSize",1);
        param.put("startTime",times.get("start"));
        param.put("endTime",times.get("end"));
        return param;

    }

    //查询水质设备数据
    private String findSzDeviceData(String deviceNo) {

        String token = getToken();
        String url = iotConfig.getUrl() + "/product/productDevice/getDeviceCurrentData";

        try {
            Map<String,String> header = new HashMap<>();
            header.put("Authorization",token);
            header.put("cvn-user-id",redisCache.getCacheObject("YSSYUSERID"));
            header.put("issuer","enroll");
            JSONObject param = createParams(deviceNo);
            String result = HttpRequest.post(url).addHeaders(header).body(param.toJSONString()).execute().body();
            JSONObject jsonObject = JSON.parseObject(result);
            if (!"200".equals(jsonObject.getString("code"))) {
                log.info("请求绿色云农接口失败:{},{}", jsonObject.getString("msg"),url);
            } else {
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //查询墒情设备数据
    private String findSqDeviceData(String deviceNo) {
        String token = getToken();
        String url = iotConfig.getUrl() + "/product/productDevice/getDeviceCurrentData";
        try {
            Map<String,String> header = new HashMap<>();
            header.put("Authorization",token);
            header.put("cvn-user-id",redisCache.getCacheObject("YSSYUSERID"));
            header.put("issuer","enroll");
            JSONObject param = createParams(deviceNo);
            String result = HttpRequest.post(url).addHeaders(header).body(param.toJSONString()).execute().body();
            JSONObject jsonObject = JSON.parseObject(result);
            if (!"200".equals(jsonObject.getString("code"))) {
                log.info("请求绿色云农接口失败:{},{}", jsonObject.getString("msg"),url);
            } else {
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    // 获取指定年指定月的开始天数和结束天数
    private Map<String, String> getFirstDayAndLastDayOfTheSpecifiedMonth(String date) {
        String[] days = date.split("-");
        // 获取当前分区的日历信息(这里可以使用参数指定时区)
        Calendar calendar = Calendar.getInstance();
        // 设置年
        calendar.set(Calendar.YEAR, Integer.parseInt(days[0]));
        // 设置月，月份从0开始
        calendar.set(Calendar.MONTH, Integer.parseInt(days[1]) -1);
        // 设置为指定月的第一天
        calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(days[2]));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        // 获取指定月第一天的时间
        Date start = calendar.getTime();
        // 设置日历天数为当前月实际天数的最大值，即指定月份的最后一天
        // 设置年
        calendar.set(Calendar.YEAR, Integer.parseInt(days[0]));
        // 设置月，月份从0开始
        calendar.set(Calendar.MONTH, Integer.parseInt(days[1])-1);
        // 设置为指定月的第一天
        calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(days[2]));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        // 获取最后一天的时间
        Date end = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 设置返回信息,返回样式根据需求自行格式化
        Map<String, String> dateMap = new HashMap<>();
        dateMap.put("start", format.format(start));
        dateMap.put("end", format.format(end));
        return dateMap;
    }


    /**
     * 获取token更新的时候通过token获取数据
     */

    private String getToken(){

        String yssytoken = redisCache.getCacheObject("YSSYTOKEN");
        if(com.ruoyi.common.utils.StringUtils.isNotEmpty(yssytoken)) {
            return yssytoken;
        }

        String token = "";
        Map<String,String> params = new HashMap<>();
        params.put("username",iotConfig.getUsername());
        params.put("password",iotConfig.getPassword());
        params.put("issuer","enroll");
        params.put("style","1");

        try {
            String result = HttpUtil.post(iotConfig.getUrl() + "/user/passport/login", JSON.toJSONString(params));
            log.debug("请求返回的结果："+result);
            JSONObject jsonObject = JSON.parseObject(result);
            if("200".equals(jsonObject.getString("status"))) {
                JSONObject data = jsonObject.getJSONObject("data");
                token = data.getString("token");
                redisCache.setCacheObject("YSSYUSERID",data.getString("userid"),24,TimeUnit.HOURS);
                redisCache.setCacheObject("YSSYTOKEN",token,24,TimeUnit.HOURS);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return token;
    }

}
